Ext.onReady(function() {
	var toolbar = Ext.create('Ext.toolbar.Toolbar', {
		dock : 'top',
		border: false,
		bodyBorder: false,
		items : [ {
			text : '新建',
			xtype : 'button',
			listeners: {
				click: function(){
					document.location.href = "/sysMgr/user_add";
				}
			}
		},'-', {
			text : '删除',
			xtype : 'button',
			listeners:{
				click:function(){
					var grid = Ext.getCmp("user_grid");
					var selectionModel = grid.getSelectionModel();
					var models = selectionModel.getSelection();
					var ids = [];
					for(var i=0; i<models.length;i++){
						ids[i] = models[i].get("Id");
					}
					Ext.Ajax.request({
						url: '/user/delete',
						type: 'POST',
						params:{
							ids: ids
						},
						callback:function(options,success, response){
							if(success){
								var result  = Ext.decode(response.responseText);
								if(result.success){
									Ext.getCmp("user_grid").getStore().reload();
								}
								else{
									Ext.Msg.alert('错误', result.msg);									
								}
							}
						}
					});
				}
			}
		},{
			text : '刷新',
			xtype : 'button',
			listeners:{
				click:function(){
					Ext.getCmp("user_grid").getStore().reload();
				}
			}
		}]
	});
	
	var store = Ext.create('Ext.data.Store', {
	    fields:['Id', 'Username', 'Name', {
	    	name: "Department", type: 'object'
	    },{
	    	name: "Sex", type: 'int'
	    },{
	    	name: "Position", type: 'object'
	    },{
	    	name: "Flight", type: "object"
	    },{
	    	name: "Education", type: 'object'
	    },/*"RFID",*/"DocumentNumber","TwoInchPhotos","Recruitment","HomeAddress",{
	    	name: "ToPartInTheWorkTime",  type: 'date'
	    }, "ContactNumber" ,{
	    	name: "Birthday", type: 'date'
	    },{
	    	name: "PoliticalStatus", type: "object"
	    },{
	    	name: "Code", type: "int"
	    }],
	    pageSize: 25,
	    autoLoad:true,
	    proxy: {
	        type: 'ajax',
	        url: '/user/all',
	        reader: {
	            type: 'json',
	            root: 'data.rows',
	            totalProperty: 'data.total'
	        }
	    }
	});

	var grid = Ext.create('Ext.grid.Panel', {
		id: "user_grid",
		margin: '0 5',
		selType : 'checkboxmodel',
		store: store,
		columns : [ {
			text : '序号',
			xtype : 'rownumberer',
			width : 40,
			sortable : false
		}, {
			text : '姓名',
			flex: 1,
			dataIndex : 'Name'
		}, {
			text : '性别',
			flex: 1,
			dataIndex : 'Sex',
			renderer: function(v){
				return v==1?"男":"女";
			}
		}, {
			text : '部门',
			flex: 1,
			dataIndex : 'Department',
			renderer:function(v){
				return v.Name;
			}
		},{
			text: '班组',
			flex: 1,
			dataIndex: 'Flight',
			renderer:function(v){
				return v.Name;
			}
		},{
			text: '职位',
			dataIndex: 'Position',
			flex: 1,
			renderer: function(v){
				return v.Name;
			}
		},{
			text: '学历',
			dataIndex: 'Education',
			flex: 1,
			renderer:function(v){
				return v.Name;
			}
		},{
			text: "详细",
			width: 60,
			renderer: function(v, metaData, record){
				return '<a href="user/detail/'+record.get("Id")+'">详细</a>';
			}
		}],
		bbar : Ext.create('Ext.PagingToolbar', {
							store : store,
							border: false,
							displayInfo : true,
							displayMsg : '显示{0} - {1} / 总数 {2}',
							emptyMsg : "没数据"
						})
	});

	var panel = Ext.create("Ext.panel.Panel", {
		border : false,
		bodyBorder : false,
		layout : 'fit',
		dockedItems : [ toolbar,{
			dock : 'top',
			border: false,
			bodyBorder: false,
			xtype: 'toolbar',
			margin: '0 5',
			items: [{
				xtype: 'textfield',
				id: "search_name",
				labelWidth:40,
				fieldLabel: '姓名'
			},{
				xtype: 'button',
				text: "搜索",
				listeners:{
					click: function(){
						grid.getStore().clearFilter(true);
						grid.getStore().filter("Name", Ext.getCmp("search_name").getValue());
					}
				}
			}]
		}],
		items : [ grid ]
	});
	Ext.create('Ext.Viewport', {
		layout : 'fit',
		style : 'background:none;background-color:white;',
		items : [ panel ]
	});
});